﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Opdracht2
{
    public partial class Form1 : Form
    {
        private string connString;
        private SqlConnection connection;

        public Form1(String connString)
        {
            this.connString = connString;
            InitializeComponent();
            connection = new SqlConnection(connString);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'boekenDataSet.Uitgever' table. You can move, or remove it, as needed.
            this.uitgeverTableAdapter.Fill(this.boekenDataSet.Uitgever);
            // TODO: This line of code loads data into the 'boekenDataSet.Categorie' table. You can move, or remove it, as needed.
            this.categorieTableAdapter.Fill(this.boekenDataSet.Categorie);
            // TODO: This line of code loads data into the 'boekenDataSet.Boeken' table. You can move, or remove it, as needed.
            this.boekenTableAdapter1.Fill(this.boekenDataSet.Boeken);
            // TODO: This line of code loads data into the 'boekenHandelDbDataSet.Boeken' table. You can move, or remove it, as needed.
            this.boekenTableAdapter.Fill(this.boekenHandelDbDataSet.Boeken);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection.Open();

            string basisQuery = "SELECT * FROM BOEKEN WHERE 1=1";

            if (checkBox_titel.Checked) basisQuery += " AND titel LIKE @titel";
            if (checkBox_isbn.Checked) basisQuery += " AND isbnnr LIKE @isbnnr";
            if (checkBox_categorie.Checked) basisQuery += " AND categoriecode LIKE @categoriecode";
            if (checkBox_uitgever.Checked) basisQuery += " AND uitgevercode LIKE @uitgevercode";

            SqlCommand command = new SqlCommand(basisQuery, connection);
            command.Parameters.AddWithValue("@titel", "%" + textBox_titel.Text + "%");
            command.Parameters.AddWithValue("@isbnnr", "%" + textBox_isbn.Text + "%");
            command.Parameters.AddWithValue("@categoriecode", "%" + comboBox_categorie.SelectedValue + "%");
            command.Parameters.AddWithValue("@uitgevercode", "%" + comboBox_uitgever.SelectedValue + "%");

            DataTable table = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(table);
            dataGridView1.DataSource = table;

            connection.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            connection.Open();
            string query = "SELECT * FROM Boeken WHERE titel LIKE @titel";
            SqlCommand command = new SqlCommand(query, connection);
            string titel = dataGridView1.SelectedCells[0].Value.ToString();
            command.Parameters.AddWithValue("@titel", titel);

            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                details.Text = (string)reader["titel"] + "(" + reader["isbnnr"].ToString() + ")\n\n"
                + "Uitgever: " + reader["uitgevercode"].ToString() + "\tCategorie: " + reader["categoriecode"].ToString();
            }
            connection.Close();
        }

        private void checkBox_titel_CheckedChanged(object sender, EventArgs e)
        {
            textBox_titel.Enabled = checkBox_titel.Checked;
        }

        private void checkBox_isbn_CheckedChanged(object sender, EventArgs e)
        {
            textBox_isbn.Enabled = checkBox_isbn.Checked;
        }

        private void checkBox_categorie_CheckedChanged(object sender, EventArgs e)
        {
            comboBox_categorie.Enabled = checkBox_categorie.Checked;
        }

        private void checkBox_uitgever_CheckedChanged(object sender, EventArgs e)
        {
            comboBox_uitgever.Enabled = checkBox_uitgever.Checked;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                string titel = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string categorie = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string prijs = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string[] newRow = new string[] { titel, categorie, prijs };
               
                dataGridView2.Rows.Add(newRow);
                berekenPrijs();
                berekenAantalBoeken();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                dataGridView2.Rows.Remove(dataGridView2.SelectedRows[0]);
                berekenPrijs();
                berekenAantalBoeken();
            }
        }

        private void berekenPrijs()
        {
            double som = 0.0;
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                som += Convert.ToDouble(row.Cells[2].Value);
            }
            label_prijs.Text = som.ToString();
        }

        private void berekenAantalBoeken()
        {
            label_boekaantal.Text = (dataGridView2.Rows.Count - 1).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            berekenAantalBoeken();
        }
    }
}
